package com.web.observers;

import java.util.Arrays;
import java.util.Observable;
import java.util.Observer;
import java.util.function.Supplier;

/**
 * @Author: wws
 * describe:
 */
public class RequestResponseObserver implements Observer, Supplier<Boolean> {

    private Observable observable;
    private byte[] frame;


    public RequestResponseObserver(byte[] frame) {
        this.frame = frame;
    }

    @Override
    public void update(Observable o, Object arg) {
        this.observable = o;
        byte[] data = (byte[]) arg;

        if (data == null) {
            return;
        }

        if (Arrays.equals(frame, data)) {
            synchronized (this) {
                notify();
            }
//            lock.lock();
//            condition.signal();
//            lock.unlock();
            System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbbbbb");

        }

    }

    @Override
    public Boolean get() {
        synchronized (this) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


//        lock.lock();
//        try {
//            condition.await();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } finally {
//            lock.unlock();
//        }

        System.out.println("ccccccccccccccccccccccccccccccc");
        observable.deleteObserver(this);
        return true;

    }



}
